package java8.atguigu.teacher.lambda06.stream01;

import java8.atguigu.lu.l01.Employee;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program lambda
 * @description:
 * @author: zhanglu
 * @create: 2019-05-27 22:07:00
 */
public class TestStreamAPI04 {

    /*
        给定一个数字列表，如何返回一个由每个数的平方构成的列表呢
        给定[1,2,3,4,5]应该返回[1,4,9,16,25]
     */
    @Test
    public void test01(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream().map(x -> x*x).forEach(System.out::println);
        Optional<Integer> o = list.stream().reduce((x,y) -> x+y);
        System.out.println(o.get());
    }

    List<Employee> employeeList = Arrays.asList(
            new Employee("张1",20,100),
            new Employee("张1",20,100),
            new Employee("张2", 21, 101),
            new Employee("张3", 22, 102),
            new Employee("张4", 23, 103),
            new Employee("张5", 24, 104),
            new Employee("张6", 25, 105)
    );

    @Test
    public void test02(){
        Optional<Integer> count = employeeList.stream().map(e -> 1)
                .reduce(Integer::sum);
        System.out.println(count.get());
    }

    List<Transctions> transctions = Arrays.asList(
            new Transctions("01", "张三1", 2010, 20d),
            new Transctions("02", "张三2", 2010, 21d),
            new Transctions("01", "张三3", 2011, 202d),
            new Transctions("04", "张三4", 2011, 23d),
            new Transctions("03", "张三5", 2012, 25d),
            new Transctions("05", "张三6", 2013, 45d),
            new Transctions("04", "张三7", 2014, 25d),
            new Transctions("04", "张三9", 2019, 34d),
            new Transctions("04", "张三10", 2017, 76d),
            new Transctions("04", "张三11", 2018, 25d),
            new Transctions("08", "张三8", 2014, 5785d)
    );

    /*
        找出2011年以后发生的所有交易，并按交易额排序 低->高
     */
    @Test
    public void test03(){
        transctions.stream().filter((o) -> o.getYear() > 2011)
                .sorted((o1, o2) ->  Double.compare(o1.getMoney(), o2.getMoney())).forEach(System.out::println);
    }


    /*
        交易员都在哪些不同的城市工作过
     */
    @Test
    public void test04(){
//        transctions.stream().collect(Collectors.groupingBy(o -> o.getNum()))
//                .forEach((k,v) -> {
//                    System.out.println(k);
//                    System.out.println(v);
//                });

        transctions.stream().map(t -> t.getNum())
                .distinct().sorted((o1, o2) -> Integer.valueOf(o2) - Integer.valueOf(o1)).forEach(System.out::println);
    }

    /*
       查找所有来自剑桥的交易员，并按姓名排序
    */
    @Test
    public void test05(){
        transctions.stream().filter((o) -> o.getNum().equals("04"))
                .sorted((t1, t2) -> t2.getName().compareTo(t1.getName()))
                .forEach(System.out::println);
    }

    /*
        返回所有交易员的姓名字符串，按字母顺序排序
     */
    @Test
    public void test06(){
        transctions.stream().map(t -> t.getName())
                .sorted()
                .forEach(System.out::println);

        String s = transctions.stream().map(t -> t.getName())
                .sorted()
                .reduce("", (x, y) -> x.concat(y));
        System.out.println(s);

        String s1 = transctions.stream().map(Transctions::getName)
                .sorted()
                .collect(Collectors.joining(",","=", "-"));
        System.out.println(s1);

        transctions.stream().map(Transctions::getName)
                .flatMap(TestStreamAPI04::filterCharacter)
                .sorted()
                .forEach(System.out::println);

    }

    public static Stream<Character> filterCharacter(String str){
        List<Character> list = new ArrayList<>();
        for(Character c : str.toCharArray()){
            list.add(c);
        }
        return list.stream();
    }


    /*
       有没有交易员在丽兰工作
    */
    @Test
    public void test07(){
       boolean b1 = transctions.stream().anyMatch(t -> t.getNum().equals("04"));
        System.out.println(b1);
    }

    /*
       去除04的总交易额
    */
    @Test
    public void test08(){
        Double d1 = transctions.stream().filter(o -> o.getNum().equals("04"))
                .map(o -> o.getMoney())
                .reduce(0d, (x, y) -> x + y);
        System.out.println(d1);

        Optional<Double> d2 = transctions.stream().filter(o -> o.getNum().equals("04"))
                .map(o -> o.getMoney())
                .reduce(Double::sum);
        System.out.println(d2.get());


    }

    /*
      所有交易中最高的
     */
    @Test
    public void test09(){
        Optional<Double> op1 = transctions.stream().map(Transctions::getMoney)
                .max(Double::compareTo);
        System.out.println(op1.get());

        Optional<Double> op2 = transctions.stream().map(o -> o.getMoney()).collect(Collectors.maxBy((o1, o2) -> Double.compare(o2, o1)));
        System.out.println(op2.get());

        DoubleSummaryStatistics ds = transctions.stream().map(Transctions::getMoney).collect(Collectors.summarizingDouble(o -> o));
        System.out.println(ds.getMax());
    }

    /*
      最小的交易
     */
    @Test
    public void test10(){
        Optional<Transctions> op1 = transctions.stream().min((o1, o2) -> Double.compare(o2.getMoney(), o1.getMoney()));
        System.out.println(op1.get());
    }


}
